/**
 * @author prateekpatil
 * @author kevingomes
 */
import java.io.*;
import java.net.*;

public class ClientThread extends Thread {
    private Socket socket = null;

    private Client clientObj;

    /**
     * IP address of the remote client.
     */
    private String remoteIp;

    public ClientThread(Socket socket, Client clientObj) {
	super("ClientThread");
	this.socket = socket;
	this.clientObj = clientObj;

	String remoteIp = socket.getInetAddress().getHostAddress();
	this.remoteIp = remoteIp;

	System.out.println("Client Thread Remote IP: "+this.remoteIp);
    }

    public void run() {
	try {
	    //PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
	    BufferedReader in = new BufferedReader(
				    new InputStreamReader(
				    this.socket.getInputStream()));

	    String inputLine, outputLine;

	    while ((inputLine = in.readLine()) != null) {
		if (inputLine.equals("Bye")) {
		    break;
		} else {
		    clientObj.processQueryHit(inputLine, this.remoteIp);
		}
	    }

	    //out.close();
	    in.close();
	    socket.close();
	} catch (IOException e) {
	    e.printStackTrace();
	}
    }
}